package controllers.systemadmin

import javax.inject.Inject

import controllers.BaseController
import models.JsonFormatters._
import models.{Dict, DictItemQuery, DictQuery, DictWithDictItem}
import org.joda.time.DateTime
import repos.{DictItemRepository, DictRepository}

import scala.concurrent.ExecutionContext.Implicits.global

class DictController @Inject()(dictRepository: DictRepository,
                               dictItemRepository: DictItemRepository) extends BaseController {

  import dictRepository.dbConfig.driver.api._

  def list = jsonAction[DictQuery] {
    (param, request) =>
      dictRepository.filter(d => {
        Seq(param.dictCode.filter(_ != "").map(d.dictCode === _),
          param.id.map(d.id === _),
          param.dictName.filter(_ != "").map(d.dictName like _)
        ).collect { case Some(i) => i }.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
      }, param.page.getOrElse(1), param.pageSize match {
        case Some(_) => param.pageSize
        case None => Some(25)
      })
        .map { case (total, dicts) =>
          Map("success" -> true,
            "total" -> total,
            "resultCode" -> 0,
            "message" -> "list complete",
            "data" -> dicts)
        }
  }

  def add = authJsonAction[DictWithDictItem](Array("admin")) {
    (param, request) =>
      dictRepository.save(param).map {
        d =>
          Map("success" -> true,
            "resutCode" -> 0,
            "message" -> "add complete",
            "data" -> d.id)
      }
  }

  def modify = authJsonAction[DictWithDictItem](Array("admin")) {
    (param, request) =>
      dictRepository.update(param).map {
        d =>
          Map("success" -> true,
            "resultCode" -> 0,
            "message" -> "modify complete")
      }
  }

  def remove = authJsonAction[DictQuery](Array("admin")) {
    (param, request) =>
      dictRepository.remove(param.id.get).map {
        d =>
          Map("success" -> true,
            "resultCode" -> 0,
            "message" -> "remove complete")
      }
  }

  def single = authJsonAction[DictQuery](Array("admin")) {
    (param, request) =>
      dictRepository.getById(param.id.get) map {
        d =>
          Map("success" -> true,
            "message" -> "single complete",
            "data" -> d)
      }
  }

  def listDictitems = authJsonAction[DictItemQuery](Array("admin")) {
    (param, request) =>
      dictItemRepository.filter(d => {
        Seq(
          param.dictId.map(d.dictId === _),
          param.value.map(d.value === _),
          param.display.map(d.display === _)
        ).collect { case Some(i) => i }.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
      }, param.page.getOrElse(1), param.pageSize match {
        case Some(_) => param.pageSize
        case None => Some(25)
      })
        .map { case (total, dictItems) =>
          Map("success" -> true,
            "total" -> total,
            "resultCode" -> 0,
            "message" -> "list complete",
            "data" -> dictItems)
        }
  }
}
